package Gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.xml.sax.SAXException;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

import Database.*;
public class SearchDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField tfSearch;
	private JTextField tfFirstName;
	private JTextField tfLastName;
	private JTextField tfDoB;
	private JTextField tfPoB;
	private JTextField tfNationality;
	private JTextField tfAddress;
	private JTextField tfRqDe;
	private JTextField tfRqLetters;
	private JTextField tfRqNumber;
	private JTextField tfRAction;
	private JTextField tfRqDate;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			SearchDialog dialog = new SearchDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public SearchDialog() {
		setBounds(100, 100, 654, 285);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel lblInputPersonidOr = new JLabel("Input PersonID or Wanted ID");
		lblInputPersonidOr.setBounds(10, 11, 203, 14);
		contentPanel.add(lblInputPersonidOr);
		
		tfSearch = new JTextField();
		tfSearch.setBounds(191, 8, 231, 20);
		contentPanel.add(tfSearch);
		tfSearch.setColumns(10);
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panel.setBounds(285, 40, 313, 161);
		contentPanel.add(panel);
		
		JLabel label = new JLabel("Firstname");
		label.setBounds(10, 11, 59, 14);
		panel.add(label);
		
		tfFirstName = new JTextField();
		tfFirstName.setEditable(false);
		tfFirstName.setColumns(10);
		tfFirstName.setBounds(71, 8, 78, 20);
		panel.add(tfFirstName);
		
		JLabel label_1 = new JLabel("Lastname");
		label_1.setBounds(159, 11, 59, 14);
		panel.add(label_1);
		
		tfLastName = new JTextField();
		tfLastName.setEditable(false);
		tfLastName.setColumns(10);
		tfLastName.setBounds(217, 8, 86, 20);
		panel.add(tfLastName);
		
		JLabel label_2 = new JLabel("DoB");
		label_2.setBounds(10, 36, 46, 14);
		panel.add(label_2);
		
		tfDoB = new JTextField();
		tfDoB.setEditable(false);
		tfDoB.setColumns(10);
		tfDoB.setBounds(70, 33, 78, 20);
		panel.add(tfDoB);
		
		JLabel label_3 = new JLabel("PoB");
		label_3.setBounds(10, 61, 46, 14);
		panel.add(label_3);
		
		tfPoB = new JTextField();
		tfPoB.setEditable(false);
		tfPoB.setColumns(10);
		tfPoB.setBounds(70, 58, 225, 20);
		panel.add(tfPoB);
		
		JLabel label_4 = new JLabel("Nationality");
		label_4.setBounds(10, 86, 59, 14);
		panel.add(label_4);
		
		tfNationality = new JTextField();
		tfNationality.setEditable(false);
		tfNationality.setColumns(10);
		tfNationality.setBounds(70, 83, 165, 20);
		panel.add(tfNationality);
		
		JLabel label_5 = new JLabel("Address");
		label_5.setBounds(10, 114, 59, 14);
		panel.add(label_5);
		
		tfAddress = new JTextField();
		tfAddress.setEditable(false);
		tfAddress.setColumns(10);
		tfAddress.setBounds(70, 111, 165, 20);
		panel.add(tfAddress);
		
		JLabel label_6 = new JLabel("Required Action");
		label_6.setBounds(10, 47, 110, 14);
		contentPanel.add(label_6);
		
		JLabel label_7 = new JLabel("Requesting Department ");
		label_7.setBounds(10, 72, 159, 14);
		contentPanel.add(label_7);
		
		tfRqDe = new JTextField();
		tfRqDe.setEditable(false);
		tfRqDe.setColumns(10);
		tfRqDe.setBounds(10, 97, 265, 20);
		contentPanel.add(tfRqDe);
		
		tfRqLetters = new JTextField();
		tfRqLetters.setEditable(false);
		tfRqLetters.setColumns(10);
		tfRqLetters.setBounds(10, 141, 265, 20);
		contentPanel.add(tfRqLetters);
		
		JLabel label_8 = new JLabel("Requesting department letters");
		label_8.setBounds(10, 125, 235, 14);
		contentPanel.add(label_8);
		
		tfRqNumber = new JTextField();
		tfRqNumber.setEditable(false);
		tfRqNumber.setColumns(10);
		tfRqNumber.setBounds(10, 181, 265, 20);
		contentPanel.add(tfRqNumber);
		
		tfRAction = new JTextField();
		tfRAction.setEditable(false);
		tfRAction.setBounds(111, 44, 164, 20);
		contentPanel.add(tfRAction);
		tfRAction.setColumns(10);
		
		JLabel label_9 = new JLabel("Requesting department number");
		label_9.setBounds(10, 166, 235, 14);
		contentPanel.add(label_9);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				XMLProvider.LoadXML();
				String id = tfSearch.getText();
				try {
					Wanted wanted = XMLProvider.getWantedbyPersonID(id);
					if(wanted == null)
					{
						wanted = XMLProvider.getWantedbyWantedID(id);
					}
					if(wanted == null)
						JOptionPane.showMessageDialog(null, "Cannot find Wanted");
					else
					{
						Person person = XMLProvider.GetPersonbyID(wanted.getPersonID());
						tfAddress.setText(person.getAddress());
						tfDoB.setText(person.getDoB()); 
						tfFirstName.setText(person.getfirstName());
						tfLastName.setText(person.getlastName());
						tfNationality.setText(person.getNationality());
						tfPoB.setText(person.getPoB());
						
						switch (wanted.getRequireAction()){
							case 1:
								tfRAction.setText("Must be Arrested");
								break;
							case 2:
								tfRAction.setText("Notify to Check");
								break;
							case 3:
								tfRAction.setText("Prevent from Travelling");
								break;
							case 4:
								tfRAction.setText("Put on Probation");
								break;
							case 5:
								tfRAction.setText("Release");
								break;
						}
						tfRqDe.setText(wanted.getDepartmentName());
						tfRqLetters.setText(wanted.getDepartmentLetter());
						tfRqNumber.setText(wanted.getDepartmentNumber());
						tfRqDate.setText(wanted.getRequestDate());
					}
				} catch (XPathExpressionException
						| ParserConfigurationException | SAXException
						| IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		btnSearch.setBounds(432, 7, 86, 23);
		contentPanel.add(btnSearch);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		btnCancel.setBounds(528, 7, 86, 23);
		contentPanel.add(btnCancel);
		
		JLabel lblRequestDate = new JLabel("Request Date");
		lblRequestDate.setBounds(10, 211, 86, 14);
		contentPanel.add(lblRequestDate);
		
		tfRqDate = new JTextField();
		tfRqDate.setEditable(false);
		tfRqDate.setBounds(111, 208, 164, 20);
		contentPanel.add(tfRqDate);
		tfRqDate.setColumns(10);
	}
}
